(function () {
    //出借面板的搜索按钮
    $("#rental_serach_btn").on("click", function () {
        $("#rental_serach_warning").addClass("hidden");
        //搜索input中的内容,去除2边空格
        let searchContent = $.trim($(this).prev("input").val());
        //搜索类型
        let searchType = $(this).prev().prev("select").val();

        if (searchContent.length == 0) {//没输入 或者只输入空格
            $("#rental_serach_warning").removeClass("hidden");
            $("#rental_serach_warning_msg").text("请输入查询内容");
        } else {
            switch (searchType) {
                case "byFilmTitle": //通过电影名
                    if (/\W+/.test(searchContent)) {//匹配所有特殊符号包括空格，等价于[^A-Za-z0-9]
                        $("#rental_serach_warning").removeClass("hidden");
                        $("#rental_serach_warning_msg").text("不能输入特殊字符");
                    } else {
                        searchByType(searchType, searchContent);
                    }
                    break;
                case "byFilmId": //通过电影id
                    if (/[^0-9]+/gi.test(searchContent)) { //匹配所有非数字,数字中间夹有空格也不行
                        $("#rental_serach_warning").removeClass("hidden");
                        $("#rental_serach_warning_msg").text("只能输入数字");
                    } else {
                        searchByType(searchType, searchContent);
                    }
                    break;
                case "byFilmType":  //通过电影类型
                    if (/\W+/.test(searchContent)) {//匹配所有特殊符号包括空格，等价于[^A-Za-z0-9]
                        $("#rental_serach_warning").removeClass("hidden");
                        $("#rental_serach_warning_msg").text("不能输入特殊字符");
                    } else {
                        searchByType(searchType, searchContent);
                    }
                    break;
                case "byFilmRating": //通过电影等级
                    if (/\W+/.test(searchContent)) {//匹配所有特殊符号包括空格，等价于[^A-Za-z0-9]
                        $("#rental_serach_warning").removeClass("hidden");
                        $("#rental_serach_warning_msg").text("不能输入特殊字符");
                    } else {
                        searchByType(searchType, searchContent);
                    }
                    break;
                case "byActorName": //通过演员名
                    if (/\W+/.test(searchContent)) {//匹配所有特殊符号包括空格，等价于[^A-Za-z0-9]
                        $("#rental_serach_warning").removeClass("hidden");
                        $("#rental_serach_warning_msg").text("不能输入特殊字符");
                    } else {
                        searchByType(searchType, searchContent);
                    }
                    break;
            }
        }

    })
})();

/**
 * @param searchType 搜索类型byFilmTitle,byFilmId,byFilmType,byFilmRating,byActorName
 * @param searchContent 搜索内容
 */
function searchByType(searchType, searchContent) {
    $.ajax({
        url: projectName + "/storeController/searchByType",
        type: "POST",
        data: {"searchType": searchType, "searchContent": searchContent},
        success: function (result) {
            fillByActorName(result, searchContent);
        }
    });
}

//填充byActorName的表格
//searchContent用来高亮搜索结果
//result格式应该是:
//[{actorId: 1, firstName: "PENELOPE", lastName: "GUINESS", fullName: "PENELOPE  GUINESS", lastUpdate: null},{},{}......]
function fillByActorName(result, searchContent) {

    let tab = $("#byActorName_table");
    tab.removeClass("hidden");
    let tbody = tab.children("tbody");
    tbody.empty();
    if (result.length > 0) {
        let reg = new RegExp(searchContent, "i");
        let tempStr = null;
        for (let i = 0; i < result.length; i++) {
            tempStr = result[i]["fullName"];
            //高亮搜索内容,replace不会改变原数组
            tempStr = tempStr.replace(reg, ("<em style='font-style: normal;color: red'>" + tempStr.match(reg) + "</em>"));
            //第一个td为隐藏域,放actorId
            tbody.append(
                "<tr>\n" +
                "    <td class=\"hidden\">" + result[i]["actorId"] + "</td>\n" +
                "    <td class=\"text-center\">" + tempStr + "</td>\n" +
                "    <td class=\"text-center\"><a class='byActorName_fold' href=\"javascript:;\">参演影片 <span class=\"glyphicon glyphicon-arrow-down\"></span></a></td>\n" +
                "</tr>"
            );
        }

        //绑定展开的单击事件
        $("#byActorName_table .byActorName_fold").click( function () {
            //当前行的tr
            let tr = $(this).parent().parent();
            let nextTr = $(this).parent().parent().next(".child_ele");
            if (nextTr.length == 0) {
                //获取隐藏域的演员id actorId
                let actorId = tr.children("td").eq(0).text();
                let howManyTr="";
                $.ajax({
                    url: projectName+"/storeController/getWhichFilmTheActorWereInvolvedIn",
                    type: "POST",
                    async:false,
                    data: {"actorId":actorId},
                    success:function (result) {
                        for (let i=0;i<result.length;i++){
                            let disabled="";
                            let color="";
                            if (result[i]["isRental"]=="已借出"){
                                disabled="disabled";
                                color="style=\"color: red;\"";
                            }
                            howManyTr+=
                                "<tr>\n" +
                                "<td class=\"hidden\">"+result[i]["inventoryId"]+"</td>" +
                                "<td class=\"text-center\">"+result[i]["filmId"]+"</td>" +
                                "<td class=\"text-center\">"+result[i]["title"]+"</td>" +
                                "<td class=\"text-center\">"+result[i]["rating"]+"</td>" +
                                "<td class=\"text-center\">"+result[i]["category"]+"</td>" +
                                "<td class=\"text-center\""+color+">"+result[i]["isRental"]+"</td>" +
                                "<td class=\"text-center\"><button class=' btn btn-info btn-sm byActorName_btn_rental'"+disabled+">借出</button></td>" +
                                "</tr>" ;
                        }
                    }
                });
                tr.after(
                    "<tr class='child_ele'>\n" +
                    "<td colspan=\"7\">\n" +
                    "<table class=\"table  table-striped table-bordered table-hover table-condensed\">\n" +
                    "<thead>\n" +
                    "<tr>\n" +
                    "<th class=\"text-center\"><a href=\"javascript:;\">编号</a></th>\n" +
                    "<th class=\"text-center\"><a href=\"javascript:;\">标题</a></th>\n" +
                    "<th class=\"text-center\"><a href=\"javascript:;\">类型</a></th>\n" +
                    "<th class=\"text-center\"><a href=\"javascript:;\">等级</a></th>\n" +
                    "<th class=\"text-center\"><a href=\"javascript:;\">状态 </a></th>\n" +
                    "<th class=\"text-center\"><a href=\"javascript:;\">操作</a></th>\n" +
                    "</tr>\n" +
                    "</thead>\n" +
                    "<tbody>\n" +
                    howManyTr+
                    "</tbody>\n" +
                    "</table>\n" +
                    "</td>\n" +
                    "</tr>"
                );

                //绑定借出按钮
                $(".byActorName_btn_rental").click(function () {
                    $("#rental_modal .modal-footer .alert").attr("hidden","hidden");
                     //全局变量
                     thisTr=$(this).parent().parent("tr");

                    //库存id
                    let inventoryId = $(this).parent().parent("tr").children("td").eq(0).text();
                    //电影名字
                    let filmTitle = $(this).parent().parent("tr").children("td").eq(2).text();
                    //情况需要填写的部分
                    $("#rental_modal_Body").find("input").eq(1).val("");
                    //填充模态框
                    let formGroup = $("#rental_modal_Body").find(".form-group");
                    formGroup.eq(0).find("input").val(filmTitle);//填充电影名称
                    formGroup.eq(3).find("input").val(inventoryId);//填充库存编号
                    //执行一个laydate实例
                    laydate.render({
                        elem: "#rental_time", //指定元素
                        value:new Date(),
                        type:"datetime",
                        btns:["now","confirm"],
                        theme:'molv',
                        calendar: true,
                    });
                    //打开模态框
                    $("#rental_modal").modal({keyboard: false, backdrop: false});



                })
            } else {
                nextTr.remove();
            }
        });
    } else {
        tbody.html(
            "<tr><td colspan=\"2\">没有搜索结果</td></tr>"
        );
    }
}

//rental_modal模态框的提交按钮
(function () {
    $("#rental_modal_submit").on("click",function () {
        //获取顾客id的值
        let customerId = $.trim($("#rental_modal_Body").find("input").eq(1).val());
        //获取借出时间的值
        let rentalTime = $("#rental_modal_Body").find("input").eq(2).val();
        //获取借库存编号
        let inventoryId = $("#rental_modal_Body").find("input").eq(3).val();
        //顾客id只能是数字
        if (/^\d+$/.test(customerId)) {
            $.ajax({
                url:projectName+"/storeController/getCustomerRental",
                type:"POST",
                data:{"customerId":customerId,"rentalTime":rentalTime,"inventoryId":inventoryId},
                success:function (result) {
                    alert(result)
                    $(this).unbind("click");

                }
            });

            $("#rental_modal").modal("hide");//关闭模态框
            thisTr.children("td").eq(5).text("已借出");
            thisTr.children("td").eq(5).css("color","red");
            thisTr.children("td").eq(6).children("button").attr("disabled","disabled");
        }else {
            $("#rental_modal .modal-footer .alert").removeAttr("hidden")
            $("#rental_modal .modal-footer .alert").children("span").text("只能输入数字");

        }
    });
})();

